home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_11 / nelson / chandle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  4.0 KB  |  114 lines

  1. /* ----------------------------------------------------
  2.  *  Listing 6
  3.  *
  4.  *  chandle.cpp
  5.  *  Adds character-device specific capability to
  6.  *  class IoctlHandle
  7.  * ------------------------------------------------- */
  8.  
  9. #include <string.h>
  10. #include "chandle.h"
  11.  
  12. struct { char *dname;
  13.          int dtype;
  14.          } static dcat[] = { { "COM", 1 },{ "AUX", 1 },
  15.                              { "CON", 3 },{ "LPT", 5 },
  16.                              { "PRN", 5 },{  0, 0 }  };
  17. static int getcat( const char *dname )      {
  18.     for( int i = 0; dcat[i].dname; i++ )
  19.         if( strnicmp( dcat[i].dname, dname, 3 ) == 0 )
  20.                 break;
  21.     return  dcat[i].dtype;
  22. }
  23. charHandle::charHandle( const char *device )
  24.                         : IoctlHandle( device )     {
  25.    _cat = getcat( device );
  26. }
  27. charHandle *charHandle::Init(const char *device)    {
  28.     charHandle *obj = new charHandle( device );
  29.  
  30.     if( obj->_handle == -1 ||   //bad handle
  31.         obj->isFile()      ||   //can't be a file
  32.         obj->_cat == 0        )  {   //unknown device
  33.                 delete obj;
  34.                 return (charHandle *) 0;
  35.     }
  36.     return obj;
  37. }
  38. void charHandle::handle_info( int handle,
  39.                               unsigned info )   {
  40.    /* Set device information word */
  41.     _iregs.x.dx = info & 0x00ff;   //dh must be 0
  42.     _iregs.x.bx = handle;
  43.     int21_44h(set_handle_info);
  44. }
  45. unsigned charHandle::ioctl_data( ioctl_cmd fn,
  46.                                  unsigned count,
  47.                                  void *buffer )    {
  48.    /* Send/receive I/O Control data to char device */
  49.     _iregs.x.bx = _handle;
  50.     _iregs.x.cx = count;     //#bytes to read/write
  51.     _iregs.x.dx = FP_OFF( (void far *) buffer );
  52.     _sregs.ds   = FP_SEG( (void far *) buffer );
  53.     int21_44h(fn);          //function number (2/3)
  54.     return _oregs.x.ax;     //#bytes xfered
  55. }
  56. int charHandle::char_ioctl( char_cmd minor_code,
  57.                             void *param_block    )  {
  58.    /* Access DOS int21h/44h/0Ch for the current
  59.     * handle and device category, generic ioctl for
  60.     * character devices.
  61.     */
  62.     _iregs.x.bx = _handle;
  63.     _iregs.h.ch = (char) _cat;
  64.     _iregs.h.cl = (char) minor_code;
  65.     _iregs.x.dx = FP_OFF( (void far *) param_block );
  66.     _sregs.ds   = FP_SEG( (void far *) param_block );
  67.     int21_44h( gen_ioctl_char );
  68.     return _dos_error ? _oregs.x.ax : 0;
  69. }
  70. int charHandle::sendIoctl( unsigned *count,
  71.                            void *data       )   {
  72.    /* Send ioctl data to current device.  */
  73.     *count = ioctl_data( send_ioctl_char,
  74.                              *count, data );
  75.     return _dos_error ? _oregs.x.ax : 0;
  76. }
  77. int charHandle::readIoctl( unsigned *count,
  78.                            void *data       )   {
  79.     *count = ioctl_data( read_ioctl_char,
  80.                              *count, data );
  81.     return _dos_error ? _oregs.x.ax : 0;
  82. }
  83. unsigned charHandle::busyCount( void )      {
  84.    /* Return LPTx device busy count, 0 on error. */
  85.     unsigned count_buf;
  86.     if( _cat != 5 ) return 0;       //error
  87.     char_ioctl( get_iter_count, (void *) &count_buf );
  88.     return _dos_error ? 0 : count_buf;
  89. }
  90. unsigned charHandle::busyCount( unsigned count )    {
  91.    /* Set LPTx device busy count.  Return new count,
  92.     * or 0 on error........      */
  93.     if( _cat != 5 ) return 0;       //error
  94.     char_ioctl( set_iter_count, (void *) &count);
  95.     return busyCount();
  96. }
  97. int charHandle::getANSIdata( struct ANSIdata *dis)  {
  98.    /* Get config data for an ANSI display driver.   */
  99.     if( _cat != 3 ) return -1;       //error
  100.     dis->info = 0;
  101.     dis->block_length = 14;
  102.     char_ioctl( get_display_mode, (void *) dis);
  103.     return _dos_error ? -1 : 0;
  104. }
  105. int charHandle::setANSIdata( struct ANSIdata *dis)  {
  106.    /* Set config data for an ANSI display driver. */
  107.     if( _cat != 3 ) return -1;       //error
  108.     dis->info = 0;
  109.     dis->block_length = 14;
  110.     char_ioctl( set_display_mode, (void *) dis);
  111.     return _dos_error ? -1 : 0;
  112. }
  113. /* ----- End of File ------------------------------- */
  114.